package org.xlin.biz.controller.rest.repast;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.xlin.biz.service.menu.MenuService;
import org.xlin.biz.service.repast.RepastService;
import org.xlin.biz.util.ResponseUtil;

/**
 * 
 * 就餐信息异步请求处理类
 * @author xl
 *
 */
@RestController
@RequestMapping("/repast")
public class RepastController {
	
	Logger logger = LoggerFactory.getLogger(RepastController.class);
	
	@Resource
	RepastService repastService;
	
	@Resource
	MenuService menuService;
	
	/**
	 * 分页查询就餐记录列表
	 * @param pageable
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/list",method = RequestMethod.POST)
    public ResponseEntity<?> queryRecord4Page(@PageableDefault Pageable pageable,
			@RequestParam Map<String,Object> condition){
		logger.debug("分页查询就餐记录列表：{}",condition);
		try {
			return ResponseUtil.json(repastService.queryRecord4Page(pageable, condition), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("分页查询就餐记录列表失败：{}",e);
			return ResponseUtil.text("查询失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
    
    /**
	 * 增加就餐记录
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/oper",method = RequestMethod.POST)
	public ResponseEntity<?> addRecord(@RequestBody Map<String,Object> condition){
		logger.debug("增加就餐记录：{}",condition);
		try {
			if(repastService.hasNoPayRepast(condition)){
				return ResponseUtil.text("该桌位已存在尚未结账的就餐单，请尝试刷新页面再试。",HttpStatus.LOCKED);
			}
			return ResponseUtil.json(repastService.addRecord(condition),HttpStatus.OK);
		} catch (Exception e) {
			logger.error("增加就餐记录失败：{}",e);
			return ResponseUtil.text("保存失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	/**
	 * 更新就餐记录
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/oper",method = RequestMethod.PUT)
	public ResponseEntity<?> modfiyRecord(@RequestBody Map<String,Object> condition){
		logger.debug("更新就餐记录：{}",condition);
		try {
			int count=repastService.modfiyRecord(condition);
			if(count>0){
				return ResponseUtil.text("更新成功！", HttpStatus.OK);
			}
			return ResponseUtil.text("结账失败，请检查是否已结账。", HttpStatus.INTERNAL_SERVER_ERROR);
		} catch (Exception e) {
			logger.error("更新就餐记录失败：{}",e);
			return ResponseUtil.text("更新失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	/**
	 * 删除就餐记录
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/oper",method = RequestMethod.DELETE)
	public ResponseEntity<?> deleteRecord(@RequestParam Map<String,Object> condition){
		logger.debug("删除就餐记录：{}",condition);
		try {
			repastService.deleteRecord(condition);
			return ResponseUtil.text("删除成功！",HttpStatus.OK);
		} catch (Exception e) {
			logger.error("删除就餐记录失败：{}",e);
			return ResponseUtil.text("删除失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
    
    /**
	 * 增加就餐菜单记录
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/menu/oper",method = RequestMethod.POST)
	public ResponseEntity<?> addMenuRecord(@RequestBody List<Map<String,Object>> params){
		logger.debug("增加就餐菜单记录：{}",params);
		try {
			repastService.addMenuRecord(params);
			return ResponseUtil.json(menuService.queryMenusByRepastId((int) params.get(0).get("repastid")), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("增加就餐记录菜单失败：{}",e);
			return ResponseUtil.text("保存失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	/**
	 * 更新就餐菜单记录
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/menu/oper",method = RequestMethod.PUT)
	public ResponseEntity<?> modfiyMenuRecord(@RequestBody Map<String,Object> condition){
		logger.debug("更新就餐菜单记录：{}",condition);
		try {
			int count=repastService.modfiyMenuRecord(condition);
			if(count>0){
				return ResponseUtil.text("更新成功！", HttpStatus.OK);
			}
			return ResponseUtil.text("更新失败,请检查是否已传菜或尝试刷新。", HttpStatus.INTERNAL_SERVER_ERROR);
		} catch (Exception e) {
			logger.error("更新就餐菜单失败：{}",e);
			return ResponseUtil.text("更新失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
    
	/**
	 * 更新菜单状态为已处理
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/menu/oper",method = RequestMethod.PATCH)
	public ResponseEntity<?> modifyMenusDoState(@RequestBody Map<String,Object> condition){
		logger.debug("变更就餐菜单状态为已处理：{}",condition);
		try {
			int count=repastService.modifyMenusDoState(condition);
			if(count>0){
				return ResponseUtil.text("数据处理成功...", HttpStatus.OK);
			}
			return ResponseUtil.text("更新失败,请尝试刷新后再试。", HttpStatus.INTERNAL_SERVER_ERROR);
		} catch (Exception e) {
			logger.error("变更菜单状态失败：{}",e);
			return ResponseUtil.text("变更菜单状态失败，请联系管理员。",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	/**
	 * 删除就餐菜单记录
	 * @param condition
	 * @return
	 */
    @RequestMapping(value="/menu/oper",method = RequestMethod.DELETE)
	public ResponseEntity<?> deleteMenuRecord(@RequestParam Map<String,Object> condition){
		logger.debug("删除就餐菜单记录：{}",condition);
		try {
			repastService.deleteMenuRecord(condition);
			return ResponseUtil.text("删除成功！",HttpStatus.OK);
		} catch (Exception e) {
			logger.error("删除就餐菜单记录失败：{}",e);
			return ResponseUtil.text("删除失败！",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
    
	/**
	 * 获取就餐总金额
	 * @param repastId
	 * @return
	 */
    @RequestMapping(value="/total",method = RequestMethod.GET)
	public ResponseEntity<?> getRepastTotal(@RequestParam int repastId){
		logger.debug("获取就餐总金额：{}",repastId);
		try {
			return ResponseUtil.json(repastService.getRepastTotal(repastId),HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取就餐总金额失败：{}",e);
			return ResponseUtil.text("获取就餐总金额失败,请联系管理员。",HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
    
    /**
     * 获取需要处理的菜单
     * @return
     */
    @RequestMapping(value="/todo/menu",method=RequestMethod.GET)
    public ResponseEntity<?> queryToDoMenus(){
    	logger.debug("获取需要处理的菜单");
		try {
			return ResponseUtil.json(repastService.queryToDoMenus(), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取需要处理的菜单失败：{}",e);
			return ResponseUtil.text("获取需处理菜单失败，请联系管理员。",HttpStatus.INTERNAL_SERVER_ERROR);
		}
    }

}
